Ext.ns("Jinyi.project");
Ext.ns("Jinyi.project.Format");

Ext.SSL_SECURE_URL = '/' + appConfig.appName + '/ext/resources/images/default/s.gif';
Ext.BLANK_IMAGE_URL = '/' + appConfig.appName + '/ext/resources/images/default/s.gif';
Ext.chart.Chart.CHART_URL = '/' + appConfig.appName
		+ '/ext/resources/charts.swf';
//document.oncontextmenu=function(){return false;}
// 错误提示信息展示
Ext.form.Field.prototype.msgTarget = 'side';
Ext.Msg.minWidth = 300;
/**
 * 格式化下拉选择框 3.0版本上有效
 */
Jinyi.project.Format.comboboxRenderer = function(value) {
	var combo = this.editor;

	if (combo) {
		var record = combo.findRecord(combo.valueField, value);
		return record
				? record.get(combo.displayField)
				: combo.valueNotFoundText;
	}
	return value;
}
/**
 * 小数转百分比
 */
Jinyi.project.Format.percentRenderer = function(value) {
	var num = new Number(value);
	if ( num == -1) { 
		return "未检查";
	 }
	else if ( num < 0) {
		return "错误";
	} else {
		var valueNew = (num * 100).toFixed(1);
		return valueNew + "%";
	}
}
/**
 * 格式化下拉选择框 3.0 带有 详细信息
 */
Jinyi.project.Format.comboboxAndTextAreaRenderer = function(value, metaData,
		record) {
	var combo = this.editor;
	if (combo) {
		record = combo.findRecord(combo.valueField, value);
		var valueNew = record
				? record.get(combo.displayField)
				: combo.valueNotFoundText;
	}
	var title = "详细信息:";
	metaData.attr = 'ext:qtitle="' + title + '"' + ' ext:qtip="' + valueNew
			+ '"';
	return valueNew;
}
/**
 * 格式化 0 和 1 ,值为0时,返回'X',值为1时,返回'√',
 */
Jinyi.project.Format.zeroAndOneRenderer = function(value) {
	if (!Ext.isEmpty(value)) {
		if (value == 0) {
			return 'X';
		} else if (value == 1) {
			return '√';
		}
	}

}
/**
 * 文本域 显示详细信息
 */
Jinyi.project.Format.textAreaRenderer = function(value, metaData, record,
		rowIndex, colIndex, store) {
	var title = "详细信息:";
	metaData.attr = 'ext:qtitle="' + title + '"' + ' ext:qtip="' + value + '"';
	return value;
};

Jinyi.project.convertDate =function (v, record){
	
	return  new Date(v);
}
/**
 * 格式化时间
 */
Jinyi.project.Format.formatDateRenderer = function(value) {
	if (!value) {
		return "";
	}
	var defaultDate = "1900-01-01";
	var rendererValue = value;
	if (Ext.isDate(value)) {
		rendererValue = value.dateFormat('Y-m-d');
	} else if (Ext.isString(value)) {
		rendererValue = value;
	}
	if (rendererValue == defaultDate) {
		return "X";
	}
	return rendererValue;

};

/**
 * 格式化时间
 */
Jinyi.project.Format.formatDate = function(value) {
	if (!value) {
		return "";
	}
	var rendererValue = value;
	if (Ext.isDate(value)) {
		rendererValue = value.dateFormat('Y-m-d');
	} else if (Ext.isString(value)) {
		rendererValue = (Date.parseDate(value, 'Y年m月d日')).dateFormat('Y-m-d');
	}
	return rendererValue;

};

/**
 * 格式化时间 Y年m月d日
 */
Jinyi.project.Format.formatDateRender = function(value) {
	var defaultDate = "1900年01月01日";
	if (!value) {
		return "";
	}
	var rendererValue = value;
	if (Ext.isDate(value)) {
		rendererValue = value.dateFormat('Y年m月d日');
	} else if (Ext.isString(value)) {
		rendererValue = (Date.parseDate(value, 'Y年m月d日')).dateFormat('Y年m月d日');
	}
	if (rendererValue == defaultDate) {
		return "X";
	}
	return rendererValue;

};

/**
 * 格式化钱的展示 例如 899.22 变成899.22万元
 * 
 * @param money
 * @return
 */
Jinyi.project.Format.formatMoneyRenderer = function(money) {
	if (!money) {
		return "";
	} else {
		var moneyStr = money + "万元";
		return moneyStr;
	}
};

/**
 * 格式化桩号 将Double类型的桩号 转化为 形如K999+333类型的桩号写法 犹如0.0转化为 K0+0 犹如333转化为 K333+0
 * 
 * @param pegNum
 * @return
 */
Jinyi.project.Format.formatPegNumRenderer = function(pegNum) {


    	if(Ext.isEmpty(pegNum)) {
    		return "";
    	} 
    		if(pegNum==0){
			return "K0+000";
		}
     var   pegNumStr =pegNum+"";
		var  pegNumStrs =pegNumStr.split(".");
		 if(pegNumStrs.length >=2){
			  var decimalPoint = pegNumStrs[1];
			  var dpLength = decimalPoint.length;
			   if(dpLength == 0){
				   return "K"+pegNumStrs[0]+"+"+decimalPoint +"000";
			   } else  if(dpLength == 1){
				   return "K"+pegNumStrs[0]+"+"+decimalPoint +"00";
			   } else  if(dpLength == 2){
				   return "K"+pegNumStrs[0]+"+"+decimalPoint +"0";
			   } else  if(dpLength > 3){
			   	   return "K"+pegNumStrs[0]+"+"+decimalPoint.substring(0, 3)+"."+decimalPoint.substring(3);
			   }  else{
			     return "K"+pegNumStrs[0]+"+"+decimalPoint ;
			   }
			 }else{
			 return "K"+pegNumStrs[0]+"+000";
		 }
};
/**
 * 格式化桩号 将桩号字符串(1.2~1.3~0;2.2~3.1~0【起止桩号...】或者0~0~1.2【中心桩号1.2】) 转化为
 * 形如K1+200~K1+300~K0+000;K2+200~K3+100~K0+000【起止桩号】或者K0+000~K0+000~K1+200类型的桩号写法
 * 桩号格式由1.2转为K1+200 相邻两个桩号间用~隔开 一组桩号与另外一组桩号间用;号隔开
 * 
 * @param pegNum
 * @return
 */
Jinyi.project.Format.formatComplexPegNumRenderer = function(pegNum) {
	var result = "";
	if (pegNum == null || pegNum == "") {
		return "";
	}
	// ";"分割桩号字符串
	var pegSectionArray = pegNum.split(";");
	for (var i = 0; i < pegSectionArray.length; i++) {
		// "~"分割桩号字符串
		var pegArray = pegSectionArray[i].split("~");
		if (pegArray[1] == "0") {
			peg = new Number(pegArray[2]);
			peg = peg.mul(1000);
			var kiloPart = peg.intDiv(1000);
			var meterPart = peg % 1000;
			return "K" + kiloPart + "+" + meterPart;
		} else {
			// 起点桩号
			peg1 = new Number(pegArray[0]);
			peg1 = peg1.mul(1000);
			var kiloPart = peg1.intDiv(1000);
			var meterPart = peg1 % 1000;
			result1 = "K" + kiloPart + "+" + meterPart;
			// 终点桩号
			peg2 = new Number(pegArray[1]);
			peg2 = peg2.mul(1000);
			var kiloPart2 = peg2.intDiv(1000);
			var meterPart2 = peg2 % 1000;
			result2 = "K" + kiloPart2 + "+" + meterPart2;
			// 合并起止点桩号字符串
			result += result1 + "~" + result2;
		}
		if (i != pegSectionArray.length - 1) {
			result += ";";
		}
	}
	return result;
};
// 格式化百分比显示
Jinyi.project.Format.formatPersentRenderer = function(doubleNum) {
  
	var persent = accMul(doubleNum, 100);
	return persent + "%";
}

// 格式化百分比显示 - 特殊处理
  Jinyi.project.Format.formatPersentSpeacilRenderer =function(doubleNum) {
					            if(Ext.isEmpty(doubleNum)){
                                         return;
                                 } 
                                 if(doubleNum == 0){
                                      return 0;
                                  }
	                             var persent = accMul(doubleNum, 100);
	                                    return persent + "%";
                                }

                                
// 调用：accAdd(arg1,arg2)
// 返回值：arg1加上arg2的精确结果
function accAdd(arg1, arg2) {
	var r1, r2, m;
	try {
		r1 = arg1.toString().split(".")[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length
	} catch (e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2))
	return (arg1 * m + arg2 * m) / m
}
// 设置相加为(this).add(arg) 样式
Number.prototype.add = function(arg) {
	return accAdd(arg, this);
}

// 除法函数，用来得到精确的除法结果
// 说明：javascript的除法结果会有误差，在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
// 调用：accDiv(arg1,arg2)
// 返回值：arg1除以arg2的精确结果
function accDiv(arg1, arg2) {
	var t1 = 0, t2 = 0, r1, r2;
	try {
		t1 = arg1.toString().split(".")[1].length
	} catch (e) {
	}
	try {
		t2 = arg2.toString().split(".")[1].length
	} catch (e) {
	}
	with (Math) {
		r1 = Number(arg1.toString().replace(".", ""))
		r2 = Number(arg2.toString().replace(".", ""))
		return (r1 / r2) * pow(10, t2 - t1);
	}
}

// 给Number类型增加一个div方法，调用起来更加方便。
Number.prototype.div = function(arg) {
	return accDiv(this, arg);
}

// 乘法函数，用来得到精确的乘法结果
// 说明：javascript的乘法结果会有误差，在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
// 调用：accMul(arg1,arg2)
// 返回值：arg1乘以arg2的精确结果
function accMul(arg1, arg2) {
	var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length
	} catch (e) {
	}
	try {
		m += s2.split(".")[1].length
	} catch (e) {
	}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", ""))
			/ Math.pow(10, m)
}

// 给Number类型增加一个mul方法，调用起来更加方便。
Number.prototype.mul = function(arg) {
	return accMul(arg, this);
}

// 减法函数，用来得到精确的除法结果
// 说明：
// 调用：accSub(arg1,arg2)
// 返回值：arg1减arg2的精确结果
function accSub(arg1, arg2) {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length
	} catch (e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2));
	// last modify by deeka
	// 动态控制精度长度
	n = (r1 >= r2) ? r1 : r2;
	return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
// 给Number类型增加一个sub方法，调用起来更加方便。
Number.prototype.sub = function(arg) {
	return accSub(arg, this);
}

// 整除
function intDiv(exp1, exp2) {
	var n1 = Math.round(exp1); // 四舍五入
	var n2 = Math.round(exp2); // 四舍五入
	var rslt = n1 / n2; // 除
	if (rslt >= 0) {
		rslt = Math.floor(rslt);
	} // 返回值为小于等于其数值参数的最大整数值。
	else {
		rslt = Math.ceil(rslt);
	} // 返回值为大于等于其数字参数的最小整数。
	return rslt;
}

// 给Number类型增加一个整除方法，调用起来更加方便。
Number.prototype.intDiv = function(arg) {
	return intDiv(this, arg);
}

String.prototype.insert=function(index,str){
return this.substring(0,index)+str+this.substr(index);
}

/** 获得系统时间 */
function getCurrentDate() {
	var hours, minutes, seconds, xfile;
	var intHours, intMinutes, intSeconds;
	var intYear, intMonth, intDate;
	var curYear, curMonth, curDate;
	var today;
	today = new Date();
	intYear = today.getFullYear();
	intMonth = today.getMonth();
	intDate = today.getDate();
	curYear = intYear + "年"
	curMonth = intMonth + 1 + "月";
	curDate = intDate + "日";
	hours = intHours + ":";

	timeString = curYear + curMonth + curDate;
	return timeString;
}
function initArray() {
	this.length = initArray.arguments.length;
	for (var i = 0; i < this.length; i++) {
		this[i + 1] = initArray.arguments[i];
	}
}
function getCurrentDate2() {
	var hours, minutes, seconds, xfile;
	var intHours, intMinutes, intSeconds;
	var intYear, intMonth, intDate;
	var curYear, curMonth, curDate;
	var today;
	today = new Date();
	intYear = today.getFullYear();
	intMonth = today.getMonth();
	intDate = today.getDate();
	intHours = today.getHours();
	intMinutes = today.getMinutes();
	intSeconds = today.getSeconds();
	curYear = intYear + "年"
	curMonth = intMonth + 1 + "月";
	curDate = intDate + "日";
	hours = intHours + ":";
	if (intMinutes < 10) {
		minutes = "0" + intMinutes + ":";
	} else {
		minutes = intMinutes + ":";
	}
	if (intSeconds < 10) {
		seconds = "0" + intSeconds + "   ";
	} else {
		seconds = intSeconds + "   ";
	}
	var week = new initArray("<font   color=RED>星期日</font>",
			"<font   color=black>星期一</font>", "<font   color=black>星期二</font>",
			"<font   color=black>星期三</font>", "<font   color=black>星期四</font>",
			"<font   color=black>星期五</font>", "<font color=GREEN>星期六</font>");
	timeString = curYear + curMonth + curDate + hours + minutes + seconds
			+ "   " + week[today.getDay() + 1];
	var timeDiv = document.getElementById("timeDiv");
	if(timeDiv){
	timeDiv.innerHTML="今天是:"+ timeString
	}
}

function ProjectControl() {

};
Ext.apply(ProjectControl.prototype, {
			title : "",
			projectId : "",
			prjAddress : "",
			manageKind : "#",
			projKind : "###",
			projectGroup : "projectList",
			projectTab : "projectInfo",
			init : function(projectId, projKind, title, projectList, projectTab) {
				this.title = title;
				this.projectId = projectId;
				this.projKind = projKind;
				if (projectList) {
					this.projectGroup = projectList;
				}
				if (projectTab) {
					this.projectTab = projectTab;
				}
			},
			changeStatusBar : function(info) {
				var statusbar = Ext.getDom("statusbar");
				if (statusbar) {
					statusbar.innerHTML = info;
				}
			},
			cleanProject : function() {
				this.title = "";
				this.projectId = "";
				this.prjAddress = "";
				this.manageKind = "#";
				this.projKind = "###";
			},
			viewProject : function() {
				var projectGroup = Ext.getCmp(this.projectGroup);
				projectGroup.setActiveTab(this.projectTab);

			},
			addNewProject : function(url, manageKind, projKind, title) {

			},
			getQueryString : function (name,href) {
				var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)",
						"i");
				if (reg.test(href))
					return unescape(RegExp.$2.replace(/\+/g, " "));
				return "";
			},
			toggleFrame : function(frameItem, iframeContent, url, tabPanel) {
				var projectId = this.projectId;
				var url = url;
				if (!Ext.isEmpty(projectId)) {
					url = appConfig.contPath + url
							+ ((url.indexOf('?') != -1) ? "&" : "?")
							+ "projectId=" + projectId;
				}
				var frame = window.frames[iframeContent]
				if (frame) {
					var href = frame.document.location.href;
					var projectIdHref = this.getQueryString("projectId",href);
					if (projectIdHref === projectId) {
						return true;
					} else {
						frame.document.location.href = url;
					}
				}
				return true;
			}
		});
		
function showWindowModel(title,width,height,url,iframName,model) {
		if(model ==null  || model == 'undefined') {
			model =true
		}
    var win = new Ext.Window({
    		title: title,
            width: width,
            height:height,
            modal: true,
			resizable:false,
			labelWidth:100,
			collapsible:false, //允许缩放条
            closeAction : 'close',
            closable:true,
            plain : true,
			modal: model, 
			buttonAlign:"center",
			html : "<iframe name='"+iframName+"Content'  marginheight='0' align='left' src='"+url+"' marginwidth='0' frameborder='0' width='100%' height='100%'  scrolling='no' id='"+iframName+"Content'></iframe>"
        });
	win.show();
	 top.winOrg =win;
    return win;
}

// end Ext.onReady
function hideORGWindow(openNameId,enterpriseName) {
  var openWin = top.winOrg
	openWin.currentRecord.set("oneOrgAgent",openNameId);	
	openWin.currentRecord.set("oneOrgAgentName",enterpriseName);
	openWin.close();
};
